package org.hit.burkun.umls.relation;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;
import org.hit.burkun.umls.GeneralQuery;
import org.hit.burkun.umls.relation.UniformRelation.TYPE;

public class HpoRelationAdpater {
	public static void main(String[] args) {
		 Collection<UniformRelation> res = getHp2HpRel();
		 UniformRelationDump.dumpRelationsWithoutPrimaryId(res, false,
		 "birelation");
		 Collection<UniformRelation> res1 = getHp2OmimGeneRel();
		 UniformRelationDump.dumpRelationsWithoutPrimaryId(res1, false,
		 "birelation");
			
			Collection<UniformRelation> res2 = getHp2GeneRel();
			UniformRelationDump.dumpRelationsWithoutPrimaryId(res2, false,
					"birelation");
	}

	public static Collection<UniformRelation> getHp2HpRel() {
		// query go 2 go
		DBHelper dbh = DBHelper.getLocalBioSearch();
		dbh.connectionDB();
		Collection<String[]> rawRes = dbh.queryData(
				"select term1, term2, rel_desc from hp_acc2acc", 3);
		dbh.disconnection();
		HashSet<String> terms = new HashSet<String>();
		for (String[] line : rawRes) {
			terms.add(line[0]);
			terms.add(line[1]);
		}
		// not update unmpapped
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids", false);
		gq.begin();
		gq.quickMap(terms, Entity.TYPE.HP_ONTOLOGY);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		String head = "hp_";
		Collection<UniformRelation> res = new LinkedList<UniformRelation>();
		for (String[] line : rawRes) {
			if (mapRes.containsKey(line[0]) && mapRes.containsKey(line[1])) {
				Set<String> umls1 = mapRes.get(line[0]);
				Set<String> umls2 = mapRes.get(line[1]);
				String u1 = StringUtils.join(umls1, ",");
				String u2 = StringUtils.join(umls2, ",");
				Entity ent1 = new Entity(null, null, u1, line[0],
						Entity.TYPE.HP_ONTOLOGY);
				Entity ent2 = new Entity(null, null, u2, line[1],
						Entity.TYPE.HP_ONTOLOGY);
				String typeStr = head + line[2];
				TYPE type = TYPE.getTypeByTag(typeStr);
				if (type != null) {
					UniformRelation ur = new UniformRelation(ent1, ent2, true,
							type);
					res.add(ur);
				}
			}
		}
		return res;
	}

	public static Collection<UniformRelation> getHp2OmimGeneRel() {
		// query do 2 gene
		DBHelper dbh = DBHelper.getLocalBioSearch();
		dbh.connectionDB();
		Collection<String[]> rawRes = dbh.queryData(
				"select term_acc, gene_id,evidence from hp_omim_geneid", 3);
		dbh.disconnection();
		HashSet<String> terms1 = new HashSet<String>();
		HashSet<String> terms2 = new HashSet<String>();

		for (String[] line : rawRes) {
			terms1.add(line[0]);
			terms2.add(line[1]);
		}
		// not update unmpapped
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids", false);
		gq.begin();
		gq.quickMap(terms1, Entity.TYPE.HP_ONTOLOGY);
		gq.quickMap(terms2, Entity.TYPE.GENE_ID);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		String head = "hp_annotation_gene_";
		Collection<UniformRelation> res = new LinkedList<UniformRelation>();
		for (String[] line : rawRes) {
			if (mapRes.containsKey(line[0]) && mapRes.containsKey(line[1])) {
				Set<String> umls1 = mapRes.get(line[0]);
				Set<String> umls2 = mapRes.get(line[1]);
				String u1 = StringUtils.join(umls1, ",");
				String u2 = StringUtils.join(umls2, ",");
				Entity ent1 = new Entity(null, null, u1, line[0],
						Entity.TYPE.HP_ONTOLOGY);
				Entity ent2 = new Entity(null, null, u2, line[1],
						Entity.TYPE.GENE_ID);
				String strType = head + line[2];
				if (TYPE.getTypeByTag(strType) != null) {
					UniformRelation ur = new UniformRelation(ent1, ent2, false,
							TYPE.getTypeByTag(strType));
					res.add(ur);
				}
			}
		}
		return res;
	}

	// 官网本身自带的基因
	public static Collection<UniformRelation> getHp2GeneRel() {
		// query do 2 gene
		DBHelper dbh = DBHelper.getLocalBioSearch();
		dbh.connectionDB();
		Collection<String[]> rawRes = dbh.queryData(
				"select hp_acc, gene_id from hp_gene", 2);
		dbh.disconnection();
		HashSet<String> terms1 = new HashSet<String>();
		HashSet<String> terms2 = new HashSet<String>();

		for (String[] line : rawRes) {
			terms1.add(line[0]);
			terms2.add(line[1]);
		}
		// not update unmpapped
		GeneralQuery gq = new GeneralQuery(DBHelper.getServerData(),
				"map_umls_ids", false);
		gq.begin();
		gq.quickMap(terms1, Entity.TYPE.HP_ONTOLOGY);
		gq.quickMap(terms2, Entity.TYPE.GENE_ID);
		gq.end();
		Map<String, Set<String>> mapRes = gq.getResult();
		Collection<UniformRelation> res = new LinkedList<UniformRelation>();
		for (String[] line : rawRes) {
			if (mapRes.containsKey(line[0]) && mapRes.containsKey(line[1])) {
				Set<String> umls1 = mapRes.get(line[0]);
				Set<String> umls2 = mapRes.get(line[1]);
				String u1 = StringUtils.join(umls1, ",");
				String u2 = StringUtils.join(umls2, ",");
				Entity ent1 = new Entity(null, null, u1, line[0],
						Entity.TYPE.HP_ONTOLOGY);
				Entity ent2 = new Entity(null, null, u2, line[1],
						Entity.TYPE.GENE_ID);
				UniformRelation ur = new UniformRelation(ent1, ent2, false,
						TYPE.HP_FILE_GENE);
				res.add(ur);
			}
		}
		return res;
	}

}
